package com.zhang.service;


import com.zhang.Email1StepBootApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest(classes = Email1StepBootApplication.class)
@RunWith(SpringRunner.class)
public class SendTest {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties; //该类是邮件yml对应的配置项类,所以可通过该类拿取yml中的数据
    @Autowired
    private TemplateEngine templateEngine;

    //发送内容为纯文本
    @Test
    public void test1() throws Exception {

        //1.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setSubject("邮件标题");
        helper.setText("邮件文本内容");
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("1838482806@qq.com");//邮件接收者

        //2.发送
        javaMailSender.send(mimeMessage);
    }

    //发送内容为html
    @Test
    public void test2() throws Exception {

        //1.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setSubject("邮件标题");
        helper.setText("<h1>验证码: adf45</h1>",true);
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("1838482806@qq.com");//邮件接收者

        //2.发送
        javaMailSender.send(mimeMessage);
    }

    //发送内容为html,读取模板
    @Test
    public void test3() throws Exception {

        //1.读取html模板文件
        Context context = new Context();
        context.setVariable("name", "张飞");
        context.setVariable("posName","运维工程师");
        context.setVariable("joblevelName", "初级工程师");
        context.setVariable("departmentName", "总办");
        String mail = templateEngine.process("mail", context);//mail: 模板名  context: 模板内容



        //2.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setSubject("邮件标题");
        helper.setText(mail,true);//邮件内容
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("1838482806@qq.com");//邮件接收者

        //3.发送
        javaMailSender.send(mimeMessage);
    }

    //发送内容为html+图片
    @Test
    public void test4() throws Exception {
        //1.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("邮件标题");
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("1838482806@qq.com");//邮件接收者

        //(1)读取图片,设为邮件内容
        String id="test";
        String imgpath = "F:\\test.png";
        FileSystemResource resource = new FileSystemResource(new File(imgpath));
        helper.setText("<h1>图片:</h1>  <img src='cid:"+id+"'/>",true);
        helper.addInline(id,resource);


        //2.发送
        javaMailSender.send(mimeMessage);
    }

    //发送内容为html+附件
    @Test
    public void test5() throws Exception {
        //1.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("邮件标题");
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("1838482806@qq.com");//邮件接收者

        //(1)读取图片设置为附件
        FileSystemResource resource = new FileSystemResource(new File("F:\\test.png"));//读取资源
        helper.setText("<h1>要看图片点击附件下载</h1> ",true);//邮件内容
        helper.addAttachment("test.png",resource); //设置该资源为附件


        //2.发送
        javaMailSender.send(mimeMessage);
    }

}

























